function DeveloperTool(){}

DeveloperTool.prototype = {
	Templates : {
		Window : "<div class='dev-window'><div class='dev-window-header'></div><div class='dev-window-content'></div></div>"
	},
	Options : {

	},
	Init : function() {
		console.log("DT:Init");
		this.isShow = false;
		this.headObj = $('html>head');
		this.InitMainWindow();
		$('#developer-show_hidden').click(
				jQuery.proxy(this.ShowHideWindow,this));
		return this;
	},
	CreateWindow : function(title) {
		var win = $(this.Templates.Window).hide().appendTo(document.body);
		var winheader = $('.dev-window-header', win).text(title);
		if (win.draggable!=undefined){
		return win.draggable( {
			handle : winheader
		});
		}else{
			return win;
		}
		return this;
	},
	InitMainWindow : function() {
		this.devMainWindow = this.CreateWindow("Main window");
		var content = $('.dev-window-content', this.devMainWindow);
		// Add tools
		$('<div/>').text('Reload CSS').click(
				jQuery.proxy(this.ReloadAllCSSThisPage,this))
				.appendTo(content);
		$('<div/>').text('S/H Template name').click(
				jQuery.proxy(this.ShowHideTemplateName,this))
				.appendTo(content);
		$('<div/>').text('S/H Template params').click(
				jQuery.proxy(this.ShowHideTemplateParams,this)).appendTo(
				content);
		return this;
	},
	ShowHideTemplateParams : function() {
		if (this._show_hide_template_param == true) {
			// Hide
			$('.template-param').remove();
			this._show_hide_template_param = false;
		} else {
			// Show
			var allTemplateName = $('[t-p]');
			allTemplateName
					.each(function(elem) {
						var $e = $(this);
						var tmp = $e.attr('t-p').split('|');
						for ( var i in tmp) {
							$e
									.append('<div class="template-param">' + tmp[i] + '</div>');
						}
					});
			this._show_hide_template_param = true;
		}
		return this;
	},
	ShowHideTemplateName : function() {
		if (this._show_hide_template == true) {
			// Hide
			$('.template-name').remove();
			this._show_hide_template = false;
		} else {
			// Show
			var allTemplateName = $('[t-n]');
			allTemplateName
					.each(function(elem) {
						var $e = $(this);
						$e.append('<div class="template-name">' + $e
								.attr('t-n') + '</div>');
					});
			this._show_hide_template = true;
		}
		return this;
	},
	ShowHideWindow : function() {
		if (this.isShow) {
			this.devMainWindow.hide();
		} else {
			this.devMainWindow.show();
		}
		this.isShow = !this.isShow;
		return this;
	},
	InitTemplateDev : function() {
		console.log("DT:InitTemplateDev");
		this.headObj = $('html>head');
		$('.developer-reload-button').click(
				jQuery.proxy(this.ReloadFramesAllCSS,this));
		this.ScanCSS();
		this.ScanFrames();
		this.ChangeFrames();
		return this;
	},
	ScanFrames : function() {
		console.log("DT:ScanFrames");
		console.log("Scan Frames:");
		var objs = $('iframe');
		var contentsList = $('.developer-contents-list').empty();
		var index = 0;
		objs.each(function() {
			var id = "iframe-id_" + index;
			var item = $(this).attr("id", id);
			var src = item.attr('src');
			var contentItem = $('<div/>').addClass("developer-contents-item")
					.appendTo(contentsList);
			$('<a/>').attr("href", "#" + id).text(src).appendTo(contentItem);
			index++;
			console.log(src);
		});
		console.log("Found:" + objs.length);
		return this;
	},
	ChangeFrames : function() {
		console.log("DT:ChangeFrames");
		var cssObjs = $('link[type="text/css"]', this.headObj);
		var objs = $('iframe');
		objs.each(function() {
			var item = $(this);
			var src = item.attr('src');
			var iframeHead = item.contents().find("head");
			cssObjs.each(function() {
				$(this).clone().appendTo(iframeHead);
			});
			console.log("Change - '" + src + "'");
		});
		return this;
	},
	ScanCSS : function() {
		console.log("DT:ScanCSS");
		console.log("Scan CSS:");
		var cssObjs = $('link[type="text/css"]', this.headObj);
		var index = 0;
		cssObjs.each(function() {
			var item = $(this);
			item.attr('cssid', index);
			var href = item.attr('href');
			console.log(href);
			index++;
		});
		console.log("Found:" + cssObjs.length);
		return this;
	},
	ReloadCSSLink : function(item) {
		var value = item.attr('href');
		var cutI = value.lastIndexOf('?');
		if (cutI != -1)
			value = value.substring(0, cutI);
		item.attr('href', value + '?t=' + new Date().valueOf());
		return this;
	},
	ReloadFramesAllCSS : function() {
		console.log("DT:ReloadFramesAllCSS");
		var parentControl = this;
		var objs = $('iframe');
		objs.each(function() {
			var item = $(this);
			var src = item.attr('src');
			var iframeHead = item.contents().find("head");
			parentControl.ReloadAllCSS(iframeHead);
			console.log("Reload from '" + src + "'");
		});
		return this;
	},
	ReloadAllCSSThisPage : function() {
		this.ReloadAllCSS(this.headObj);
		return this;
	},
	ReloadAllCSS : function(headObj) {
		console.log("DT:ReloadAllCSS");
		var parentObj = this;
		$('link[type="text/css"]', headObj).each(function() {
			parentObj.ReloadCSSLink($(this));
		});
		return this;
	}
};